<?php
global $_GPC, $_W;
function getcode($code)
{
    $cityinfo = pdo_get('yzzc_sun_city', array('code' => $code));
    return $cityinfo;
}
$GLOBALS['frames'] = $this->getMainMenu();
$platform_fee = pdo_get('yzzc_sun_store_config',['uniacid'=>$_W['uniacid']],['platform_fee','payout_fee']);
$op = $_GPC['op'] ?? 'display';
if ($op == 'display') {
    $where = "where type = 1 and uniacid =" . $_W['uniacid'];
    if ($_GPC['keywords']) {
        $op = $_GPC['keywords'];
        $where .= " and name LIKE  '%$op%' or mobile like '%$op%'";
    }
    if ($_GPC['type'] == 'wait') {
        $where .= " and status =1";
    } elseif ($_GPC['type'] == 'ok') {
        $where .= " and status =2";
    }
    $page = max(1, intval($_GPC['page']));
    $size = intval($_GPC['psize']) ? intval($_GPC['psize']) : 10;
    $sql = 'SELECT * FROM ' . tablename('yzzc_sun_store_apply') . "{$where} ORDER BY store_id DESC LIMIT " . (($page - 1) * $size) . ',' . $size;

    $info = pdo_fetchall($sql);
    foreach ($info as $key => $value) {
        $info[$key]['createtime'] = date('Y-m-d H:i:s', $value['createtime']);
        $province = getcode($value['province']);
        $info[$key]['province'] = $province['fullname'] ?? '';

        $city = getcode($value['city']);
        $info[$key]['city'] = $city['fullname'] ?? '';

        $area = getcode($value['area']);
        $info[$key]['area'] = $area['fullname'] ?? '';
    }
    $total = pdo_fetchcolumn("select count(*) from " . tablename("yzzc_sun_store") . $where);
    $pager = pagination($total, $page, $size);
    include $this->template('web/branch/apply_list');
}
elseif ($op == 'edit') {
    $map_key = pdo_get('yzzc_sun_system', array('uniacid' => $_W['uniacid']))['map_key'];
    /**获取省份*/
    $province = pdo_getall('yzzc_sun_city', array('pcode' => 0), array('code', 'fullname'));

    $info = pdo_get('yzzc_sun_store_apply', ['uniacid' => $_W['uniacid'], 'store_id' => $_GPC['store_id']]);
    $info['province_name'] = getcode($info['province'])['fullname'];
    $info['city_name'] = getcode($info['city'])['fullname'];
    $info['area_name'] = getcode($info['area'])['fullname'];
    $info['pic'] = $info['pic'] ? explode(',',$info['pic']) : '';
//    if (checksubmit('submit')) {
//        $data = [
//            'uniacid' => $_W['uniacid'],
//            'name' => trim($_GPC['name']),
//            'mobile' => trim($_GPC['mobile']),
//            'province' => trim($_GPC['province']),
//            'city' => trim($_GPC['city']),
//            'area' => trim($_GPC['area']),
//            'lng' => trim($_GPC['lng']),
//            'lat' => trim($_GPC['lat']),
//            'address' => trim($_GPC['address']),
//            'driving_license' => trim($_GPC['driving_license']),
//            'id_license' => trim($_GPC['id_license']),
//            'sy_insurance' => trim($_GPC['sy_insurance']),
//            'jq_insurance' => trim($_GPC['jq_insurance']),
//            'updatetime' => time(),
//        ];
//        $result = pdo_update('yzzc_sun_store', $data, [
//            'store_id' => $_GPC['store_id'],
//            'uniacid' => $_W['uniacid']
//        ]);
//        if ($result) {
//            message('修改成功', $this->createWebUrl('store_personal', array('op' => 'display')), 'success');
//        } else {
//            message('修改失败', $this->createWebUrl('store_personal', array('op' => 'edit', 'store_id' => $_GPC['store_id'])), 'error');
//        }
//    }
    include $this->template('web/branch/apply_detail');
} elseif ($op == 'delete') {
    $store_info = pdo_get('yzzc_sun_store', array('store_id' => $_GPC['id']));
    $branch = pdo_get('yzzc_sun_branch', array('store_id' => $_GPC['id']));
    if ($store_info['status'] == 1){
        $res1 = pdo_delete('yzzc_sun_store', array('store_id' => $_GPC['id']));
        $res2 = pdo_delete('yzzc_sun_branch', array('store_id' => $_GPC['id']));
        $res3 = pdo_delete('yzzc_sun_branch_admin', array('bid' => $branch['id']));
        if ($res1 && $res3 && $res2) {
            message('操作成功', $this->createWebUrl('store_personal', array()), 'success');
        } else {
            message('操作失败', '', 'error');
        }
    }else{
        $res = pdo_delete('yzzc_sun_store', array('store_id' => $_GPC['id']));
        if ($res) {
            message('操作成功', $this->createWebUrl('store_personal', array()), 'success');
        } else {
            message('操作失败', '', 'error');
        }
    }
} elseif ($op == 'disable') {
    $res = pdo_update('yzzc_sun_store', array('is_disable' => $_GPC['is_disable']),['store_id' => $_GPC['id']]);
    if ($res) {
        message('操作成功', $this->createWebUrl('store_personal', array()), 'success');
    } else {
        message('操作失败', '', 'error');
    }

}elseif ($op == 'change') {
    $where = ['store_id' => $_GPC['id']];
    $store_info = pdo_get('yzzc_sun_store_apply', $where);
    if ($store_info['status'] == $_GPC['status']) {
        message('状态错误，修改失败', $this->createWebUrl('branch_apply', array()), 'error');
    }

    $data = ['status' => $_GPC['status']];
    $platform_fee_ = $payout_fee =0;
    if (isset($_GPC['remark']) && $_GPC['remark']) {
        $data['remark'] = $_GPC['remark'];
    }else{
        $platform_fee_ = $platform_fee['platform_fee'];
        $payout_fee = $platform_fee['payout_fee'];
        if (isset($_GPC['platform_fee']) && $_GPC['platform_fee']) {
            $platform_fee_ = $_GPC['platform_fee'];
        }
        if (isset($_GPC['payout_fee']) && $_GPC['payout_fee']) {
            $payout_fee = $_GPC['payout_fee'];
        }
    }

    try {
        pdo_fetchall('START TRANSACTION');//启动一个新事务
        $res1 = pdo_update('yzzc_sun_store_apply', $data, $where);
        if (!$res1){
            pdo_fetchall('ROLLBACK');//事务提交
            message('操作失败', $this->createWebUrl('branch_apply', array()), 'error');
        }
        if ($data['status'] == 1){
            //审核通过 创建门店
            $branch = [
                'type' => 2,
                'store_id' => $store_info['id'],
                'platform_fee' => $platform_fee_,
                'payout_fee' => $payout_fee,
                'shop_tel' => $store_info['mobile'],
                'name' => $store_info['title'],
                'province' => $store_info['province'],
                'city' => $store_info['city'],
                'area' => $store_info['area'],
                'address' => $store_info['address'],
                'lng' => $store_info['lng'],
                'lat' => $store_info['lat'],
                'ranges' => $store_info['ranges'],
                'pic' => $store_info['pic'],
                'createtime' => time(),
                'uniacid' => $store_info['uniacid']
            ];

            pdo_insert('yzzc_sun_branch',$branch);
            $branch_id = pdo_insertid();
            if (!$branch_id){
                pdo_fetchall('ROLLBACK');//事务回滚
                message('创建门店错误', $this->createWebUrl('branch_apply', array()), 'error');
            }
            //创建超级管理员
            $branch_admin = [
                'bid' => $branch_id,
                'uid' => $store_info['uid'],
                'auth'=> 1,
                'uniacid' => $store_info['uniacid'],
                'createtime' => time()
            ];

            $branch_admin_result = pdo_insert('yzzc_sun_branch_admin',$branch_admin);

            if (!$branch_admin_result){
                pdo_fetchall('ROLLBACK');//事务回滚
                message('创建门店错误', $this->createWebUrl('branch_apply', array()), 'error');
            }
        }
        pdo_fetchall('COMMIT');//事务提交
        message('操作成功', $this->createWebUrl('branch_apply', array()), 'success');
    }catch (Exception $exception){
        pdo_fetchall('ROLLBACK');//事务回滚
        message('已存在数据', '', 'error');
    }

}
